package com.woniuxy.lab.woniu_lab.mqListener;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.dockerjava.api.DockerClient;
import com.woniuxy.lab.woniu_lab.model.Exper;
import com.woniuxy.lab.woniu_lab.model.Port;
import com.woniuxy.lab.woniu_lab.service.ExperService;
import com.woniuxy.lab.woniu_lab.service.PortService;
import com.woniuxy.lab.woniu_lab.service.dto.RmRangeDto;
import com.woniuxy.lab.woniu_lab.util.DockerUtil;
import com.woniuxy.lab.woniu_lab.util.ShellUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Component
@RocketMQMessageListener(topic = "stop_container_topic",consumerGroup = "stopContainer")
@Slf4j
public class StopContainerListener implements RocketMQListener<RmRangeDto> {

    @Resource
    private PortService portService;


    @Resource
    private ExperService experService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Value("${dockerHttp}")
    private String dockerIp;
    @Value("${dockerPort}")
    private String dockerPort;

    @Value("${linux.ip}")
    private String linuxIp;
    @Value("${linux.username}")
    private String username;
    @Value("${linux.password}")
    private String password;
    @Override
    public void onMessage(RmRangeDto rmRangeDto) {
        log.info("查询容器是否释放");
        Exper exper = experService.getOne(new QueryWrapper<Exper>().eq("no",rmRangeDto.getNo()).
                eq("container_id",rmRangeDto.getContainerId()));

        if (exper==null){
            log.info("容器错误");
            return;
        }

        if (LocalDateTime.now().isBefore(exper.getReleaseTime())){
            if (!exper.getStatus().equals("CLOSE")){
                boolean before = exper.getReleaseTime().isBefore(LocalDateTime.now());
                System.out.println(before);
                if (before) {
                    log.info("释放容器");
                    //暂停容器
                    DockerUtil dockerUtil = DockerUtil.getInstance(dockerIp, dockerPort);
                    dockerUtil.stopContainer(rmRangeDto.getContainerName());
                    //删除端口
                    portService.remove(new QueryWrapper<Port>().eq("port", rmRangeDto.getPort()));
                    //修改对应实验暂停

                    exper.setStatus("CLOSE");
                    exper.setStatus("FAIL");
                    exper.setReleaseTime(LocalDateTime.now());
                    experService.updateById(exper);
                    //关闭端口
                    ShellUtil shell = ShellUtil.getShell(linuxIp, username, password);
                    shell.closePort(rmRangeDto.getPort());
                }else {
                    log.info("用户已经提前释放");
                }
            }else {
                log.info("未到期,过30分钟后查询");
                Message<RmRangeDto> message = MessageBuilder.withPayload(rmRangeDto).build();
                rocketMQTemplate.syncSend("stop_container_topic",message,3000,16);
            }
        }
    }
}
